package com.cabinet.web.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cabinet.web.entity.Seller;

import cn.com.cabinet.service.SellerServiceImpl;
import cn.com.easy.utils.CookieUtils;

@Controller
@RequestMapping("/seller/login")
public class LoginController {

	@Autowired
	private SellerServiceImpl sellerServiceImpl;
	
	
	@RequestMapping()
	public String login(HttpServletRequest request,ModelMap modelMap) throws Exception{
		String pfCount = CookieUtils.getCookieValue(request, "pfCount");
		System.out.println("loginCheck:"+pfCount);
		if(pfCount == "null" || pfCount == null){
			return "/businessLogin/login";
		}else{
			modelMap.addAttribute("pfCount", pfCount);
			return "/businessBackground/main";
		}
		
	}
	
	
	@RequestMapping("/check")
	public String SellerLogin(HttpServletRequest request, HttpServletResponse response,String sAccount,String sPwd,ModelMap modelMap) throws Exception{
		String pfCount = CookieUtils.getCookieValue(request, "pfCount");
		System.out.println("pfCount:"+pfCount);
		if(pfCount == "null" || pfCount == null){
			if(sellerServiceImpl.getSellerBysAccountAndsPwd(sAccount, sPwd)){
				//设置cookie
				CookieUtils.setCookie(request, response, "pfCount",sAccount);
				//System.out.println(CookieUtils.getCookieRedirectUrl(request));		
					modelMap.addAttribute("pfCount", sAccount);
					return "/businessBackground/main";
			}else{
				//System.out.println("false");
				modelMap.addAttribute("loginMessage", "账号不存在或密码不正确！");
				return "/businessLogin/login";
			}
		}else{
			return "redirect:"+CookieUtils.getCookieRedirectUrl(request);
		}
	}
	
	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Cookie cookie = CookieUtils.getCookie(request, "pfCount");
		CookieUtils.deleteCookie(request, response, cookie);
		return "redirect:/";
	}
	
/*	@RequestMapping()
	public String login(HttpServletRequest request,ModelMap modelMap) throws Exception{
		String adminCount = CookieUtils.getCookieValue(request, "adminCount");
		System.out.println("loginCheck:"+adminCount);
		if(adminCount == "null" || adminCount == null){
			return "/login/admin_login";
		}else{
			modelMap.addAttribute("adminCount", adminCount);
			return "/index/index";
		}
		
	}

	@RequestMapping("/loginCheck")
	public String loginCheck(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,String account , String password) throws Exception{
		String adminCount = CookieUtils.getCookieValue(request, "adminCount");
		System.out.println("loginCheck:"+adminCount);
		if(adminCount == "null" || adminCount == null){
			if(adminServiceImpl.checkManger(account, password)){
				//设置cookie
				CookieUtils.setCookie(request, response, "adminCount",account);
				//System.out.println(CookieUtils.getCookieRedirectUrl(request));		
					modelMap.addAttribute("adminCount", account);
					return "/index/index";
			}else{
				//System.out.println("false");
				modelMap.addAttribute("loginMessage", "账号不存在或密码不正确！");
				return "/login/admin_login";
			}
		}else{
			return "redirect:"+CookieUtils.getCookieRedirectUrl(request);
		}
	
		
	}
	
	*/
	
	
}
